<template>
  <div class="data-container">
    <div class="tab-list">
      <div class="item" :class="{ active: active === 0 }" @click="handleTab(0)">今日</div>
      <div class="item" :class="{ active: active === 1 }" @click="handleTab(1)">昨日</div>
      <div class="item" :class="{ active: active === 2 }" @click="handleTab(2)">上周</div>
      <div class="item" :class="{ active: active === 3 }" @click="handleTab(3)">本月</div>
      <div class="item" :class="{ active: active === 4 }" @click="handleTab(4)">上月</div>
    </div>
    <div class="date-picker">
      <el-date-picker v-model="datePicker" @change="handleChange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
    </div>
    <div class="data-list-container">
      <div class="data-panel-title">UV <span v-if="active > 1 || active === -1">(日均)</span></div>
      <div class="data-list">
        <div class="item">
          <div class="border">
            <div class="name">APP</div>
            <div class="value">{{ (uv && uv.app) || 0 }}</div>
          </div>
        </div>
        <div class="item">
          <div class="border">
            <div class="name">PC</div>
            <div class="value">{{ (uv && uv.pc) || 0 }}</div>
          </div>
        </div>
        <div class="item">
          <div class="border">
            <div class="name">M站</div>
            <div class="value">{{ (uv && uv.m) || 0 }}</div>
          </div>
        </div>
        <div class="item">
          <div class="border">
            <div class="name">小程序</div>
            <div class="value">{{ (uv && uv.wx) || 0 }}</div>
          </div>
        </div>
      </div>
      <div class="data-panel-title">PV<span v-if="active > 1 || active === -1">(日均)</span></div>
      <div class="data-list">
        <div class="item">
          <div class="border">
            <div class="name">APP</div>
            <div class="value">{{ (pv && pv.app) || 0 }}</div>
          </div>
        </div>
        <div class="item">
          <div class="border">
            <div class="name">PC</div>
            <div class="value">{{ (pv && pv.pc) || 0 }}</div>
          </div>
        </div>
        <div class="item">
          <div class="border">
            <div class="name">M站</div>
            <div class="value">{{ (pv && pv.m) || 0 }}</div>
          </div>
        </div>
        <div class="item">
          <div class="border">
            <div class="name">小程序</div>
            <div class="value">{{ (pv && pv.wx) || 0 }}</div>
          </div>
        </div>
      </div>
      <div class="data-panel-title">IP<span v-if="active > 1 || active === -1">(日均)</span></div>
      <div class="data-list">
        <div class="item">
          <div class="border">
            <div class="value">{{ ip || 0 }}</div>
          </div>
        </div>
        <div class="item opacity">
          <div class="border">
            <div class="value">{{ ip || 0 }}</div>
          </div>
        </div>
        <div class="item opacity">
          <div class="border">
            <div class="value">{{ ip || 0 }}</div>
          </div>
        </div>
        <div class="item opacity">
          <div class="border">
            <div class="value">{{ ip || 0 }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiMemberLeftCls } from '@/api/realTime/index'
import { formatDate, getDay, beginOfWeek, endOfWeek, dateOfMonth, dateOfPrevMonth } from '@/utils'

export default {
  name: 'panel1',
  data() {
    return {
      active: 0,
      datePicker: '',
      pv: {},
      uv: {},
      ip: '',
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const today = new Date()
      this.dateList = [
        { name: '今日', start: formatDate(today, 'Y-M-D'), end: formatDate(today, 'Y-M-D') },
        { name: '昨日', start: formatDate(getDay(-1), 'Y-M-D'), end: formatDate(getDay(-1), 'Y-M-D') },
        { name: '上周', start: beginOfWeek(getDay(-7)), end: endOfWeek(getDay(-7)) },
        { name: '本月', start: dateOfMonth()[0], end: dateOfMonth()[1] },
        { name: '上月', start: dateOfPrevMonth()[0], end: dateOfPrevMonth()[1] },
      ]
      this.getData()
    },
    // 获取数据
    getData() {
      let info = {}
      if (this.active >= 0) {
        info = {
          start_time: this.dateList[this.active].start,
          end_time: this.dateList[this.active].end,
        }
      } else {
        info = {
          start_time: formatDate(this.datePicker[0], 'Y-M-D'),
          end_time: formatDate(this.datePicker[1], 'Y-M-D'),
        }
      }
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      this.$emit('timeChange', info)
      apiMemberLeftCls(formData).then((res) => {
        const { pv, uv, ip, time } = res.data
        this.$emit('updateTime', time)
        this.pv = pv
        this.uv = uv
        this.ip = ip
      })
    },
    // 日期选择
    handleChange(val) {
      if (val) {
        this.active = -1
        this.getData()
      } else {
        this.active = 0
        this.getData()
      }
    },
    // tab切换
    handleTab(index) {
      this.active = index
      this.datePicker = ''
      this.getData()
    },
    onResize() {},
  },
}
</script>
<style lang="scss" scoped>
$bgColor: #060a22;
$mainColor: #68cee0;
$mainColorOpcity: rgba(104, 206, 224, 0.3);
.data-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  padding: 20px;
  overflow: hidden;
}
.tab-list {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .item {
    flex-grow: 1;
    height: 30px;
    border: 1px solid #ffffff;
    border-right: none;
    color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    &:hover {
      border-color: $mainColor;
      color: $mainColor;
    }
    &.active {
      border-color: $mainColor;
      background: $mainColorOpcity;
      font-weight: bold;
      color: $mainColor;
    }

    &:last-child {
      margin-right: 0;
      border-radius: 0 3px 3px 0;
      border: 1px solid #ffffff;
    }
    &:first-child {
      border-radius: 3px 0 0 3px;
    }
  }
}

.date-picker {
  margin-top: 10px;
}
.data-panel-title {
  margin-top: 20px;
  width: 100%;
  font-size: 16px;
  font-weight: bold;
}
.data-list-container {
  width: 100%;
  height: calc(100vh - 330px);
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
}
.data-list {
  flex-grow: 1;
  width: 100%;
  margin-top: 10px;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
  .item {
    width: 50%;
    height: 45%;
    margin-bottom: 10px;
    &:nth-child(2n + 1) {
      padding-right: 5px;
    }
    &:nth-child(2n) {
      padding-left: 5px;
    }
    .border {
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      border: 1px solid $mainColor;
      border-radius: 3px;
    }
    .name {
      position: absolute;
      left: 0;
      top: 0;
      border-right: 1px solid $mainColor;
      border-bottom: 1px solid $mainColor;
      border-radius: 0 0 3px 0;
      background: $mainColorOpcity;
      font-size: 12px;
      padding: 4px 0;
      width: 35%;
      text-align: center;
    }
    .value {
      font-size: 16px;
      color: #ffffff;
      font-weight: bold;
    }
  }
}

::v-deep .el-input__inner {
  background: none;
  width: 100%;
  color: #ffffff;
  height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
::v-deep .el-range-input {
  background: none;
  color: #ffffff;
}
::v-deep .el-input__icon,
::v-deep .el-date-editor .el-range-separator {
  line-height: 30px !important;
  color: #ffffff;
  opacity: 0.5;
}
.opacity {
  opacity: 0;
}
</style>
